package com.tao.friend.controller.user;

import com.tao.common.creo.constants.HttpConstants;
import com.tao.common.creo.controller.BaseController;
import com.tao.common.creo.domain.Result;
import com.tao.friend.domain.user.vo.LoginUserVO;
import com.tao.friend.domain.user.dto.UserDTO;
import com.tao.friend.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController extends BaseController {


    @Autowired
    IUserService userService;

    /**
     * 获取验证码
     */
    @PostMapping("/sendCode")
    public Result<Void> sendCode(@RequestBody UserDTO userDTO) {
        return toResult(userService.sendCode(userDTO));
    }

    /**
     * 登录
     * @return
     */
    @PostMapping("/code/login")
    public Result<String> codeLogin(@RequestBody UserDTO userDTO) {
        return Result.success(userService.codeLogin(userDTO.getPhone(), userDTO.getCode()));
    }

    /**
     * 获取用户信息
     * @param token
     * @return
     */
    @GetMapping("/info")
    public Result<LoginUserVO> info(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        return userService.info(token);
    }

    @DeleteMapping("/logout")
    public Result<Void> logout(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        return toResult(userService.logout(token));
    }
}
